/****************************************************************************
*
*                            Open Watcom Project
*
* Copyright (c) 2002-2019 The Open Watcom Contributors. All Rights Reserved.
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  conversion function for DOS date/time stamps
*
****************************************************************************/


// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
// OMF format files generated by C/C++ compiler use DOS date/time format
// for dependency timestamp in comment record.
//
// Resource files generated by resource compiler use time_t date/time
// format for dependency timestamp in info record.
//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


#include <time.h>
#include "dostimet.h"


enum {
    TIME_SEC_B  = 0,
    TIME_SEC_F  = 0x001f,
    TIME_MIN_B  = 5,
    TIME_MIN_F  = 0x07e0,
    TIME_HOUR_B = 11,
    TIME_HOUR_F = 0xf800
};

enum {
    DATE_DAY_B  = 0,
    DATE_DAY_F  = 0x001f,
    DATE_MON_B  = 5,
    DATE_MON_F  = 0x01e0,
    DATE_YEAR_B = 9,
    DATE_YEAR_F = 0xfe00
};

time_t _timet2dos( time_t x )
/******************************************************************/
{
    struct tm      *ltime;
    unsigned short dos_time;
    unsigned short dos_date;

    ltime = localtime( &x );
    dos_date = (unsigned short)((( ltime->tm_year - 80 ) << DATE_YEAR_B )
             | (( ltime->tm_mon + 1 ) << DATE_MON_B )
             | (( ltime->tm_mday ) << DATE_DAY_B ));
    dos_time = (unsigned short)((( ltime->tm_hour ) << TIME_HOUR_B )
             | (( ltime->tm_min ) << TIME_MIN_B )
             | (( ltime->tm_sec / 2 ) << TIME_SEC_B ));
    return( dos_date * 0x10000L + dos_time );
}

time_t _dos2timet( time_t x )
/******************************************************************/
{
    struct tm      ltime;
    unsigned short dos_time;
    unsigned short dos_date;

    dos_date = (unsigned short)( x / 0x10000L );
    dos_time = (unsigned short)( x );

    ltime.tm_year  = ( ( dos_date & DATE_YEAR_F ) >> DATE_YEAR_B ) + 80;
    ltime.tm_mon   = ( ( dos_date & DATE_MON_F ) >> DATE_MON_B ) - 1;
    ltime.tm_mday  = ( dos_date & DATE_DAY_F ) >> DATE_DAY_B;

    ltime.tm_hour  = ( dos_time & TIME_HOUR_F ) >> TIME_HOUR_B;
    ltime.tm_min   = ( dos_time & TIME_MIN_F ) >> TIME_MIN_B;
    ltime.tm_sec   = ( ( dos_time & TIME_SEC_F ) >> TIME_SEC_B ) * 2;

    ltime.tm_isdst = -1;

    return( mktime( &ltime ) );
}
